/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.dao;

import core.Course;
import core.Grade;
import core.Group;
import core.Student;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 *
 * @author Mikael
 */
public class AbstractDAO {

    protected final static SimpleJdbcTemplate jdbc =
            new SimpleJdbcTemplate(
            new DriverManagerDataSource(
            "com.mysql.jdbc.Driver",
            "jdbc:mysql://db.student.chalmers.se:3306/nilssonm",
            "nilssonm",
            "u9ThnaNj"));

    protected AbstractDAO() {
    }
    // Inner (Spring) class to map table data to Group
    protected final static ParameterizedRowMapper<Group> groupMapper =
            new ParameterizedRowMapper<Group>() {

                // The return type with respect to Java 5 covariant return types
                public Group mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Group group = new Group();
                    group.setId(rs.getLong("Groups.id"));
                    group.setName(rs.getString("Groups.name"));
                    group.setDescription(rs.getString("Groups.description"));
                    group.setCreatorId(rs.getLong("Groups.creator"));
                    group.setCreator(rs.getString("Students.fname") +
                            " " + rs.getString("Students.lname"));
                    return group;
                }
            };
    // Inner (Spring) class to map table data to Course
    protected final static ParameterizedRowMapper<Course> courseMapper =
            new ParameterizedRowMapper<Course>() {

                // The return type with respect to Java 5 covariant return types
                public Course mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Course course = new Course();
                    course.setId(rs.getLong("id"));
                    course.setName(rs.getString("name"));
                    course.setCode(rs.getString("code"));
                    course.setDescription(rs.getString("description"));
                    return course;
                }
            };
    // Inner (Spring) class to map table data to Student
    protected final static ParameterizedRowMapper<Student> studentMapper =
            new ParameterizedRowMapper<Student>() {

                // The return type with respect to Java 5 covariant return types
                public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Student student = new Student();
                    student.setId(rs.getLong("id"));
                    student.setFirstName(rs.getString("fname"));
                    student.setLastName(rs.getString("lname"));
                    student.setEmail(rs.getString("email"));
                    student.setAddress(rs.getString("address"));
                    student.setPassword(rs.getString("password"));
                    return student;
                }
            };
    // Inner (Spring) class to map table data to StudentGroupPair
    protected final static ParameterizedRowMapper<Grade> gradeMapper =
            new ParameterizedRowMapper<Grade>() {

                // The return type with respect to Java 5 covariant return types
                public Grade mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Grade grade = new Grade();
                    grade.setCourseId(rs.getLong("Courses.id"));
                    grade.setCourseName(rs.getString("Courses.name"));
                    grade.setCourseCode(rs.getString("Courses.code"));

                    int iGrade = rs.getInt("StudentCoursePairs.grade");
                    String sGrade = "";
                    if(iGrade > 2)
                        sGrade = "" + iGrade;
                    else if(iGrade == 2)
                        sGrade = "F";
                    else
                        sGrade = "-";

                    grade.setGradeText(sGrade);

                    return grade;
                }
            };
    protected final static Object[] DUMMY = new Object[]{};
}